﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;


namespace SPBusiness
{
   public sealed class Group
    {
              /// <summary>
       /// Group class has only Static Methods, no constructor needed.
       /// </summary>
       private Group() { }
        public static void AddMember(string pSiteUrl, string pWebRelativeUrl, string pGroupName, string pAccountName)
        {
         try
         {
            //SPSecurity.RunWithElevatedPrivileges(delegate()
            //{
               using (SPSite vSPSite = new SPSite(pSiteUrl))
               {
                  using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                  {
                     SPUser vSPUser = vSPWeb.EnsureUser(pAccountName);
                     if (vSPUser != null)
                     {
                        AddMember(pSiteUrl, pWebRelativeUrl, pGroupName, vSPUser);
                     }
                  }
               }
            //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Group).Namespace, typeof(Group).Name, "AddMember", ex);
                throw;
            }
        }

        public static void RemoveMember(string pSiteUrl, string pWebRelativeUrl, string pGroupName, string pAccountName)
        {
         try
         {
            //SPSecurity.RunWithElevatedPrivileges(delegate()
           // {
               using (SPSite vSPSite = new SPSite(pSiteUrl))
               {
                  using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                  {
                     SPUser vSPUser = vSPWeb.EnsureUser(pAccountName);
                     if (vSPUser != null)
                     {
                        RemoveMember(pSiteUrl, pWebRelativeUrl, pGroupName, vSPUser);
                     }
                  }
               }
            //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Group).Namespace, typeof(Group).Name, "RemoveMember", ex);
                throw;
            }
        }

        public static void ChangeMember(string pSiteUrl, string pWebRelativeUrl, string pGroupName, string pAccountNameOld, string pAccountNameNew)
        {
         try
         {
            //SPSecurity.RunWithElevatedPrivileges(delegate()
           // {
               using (SPSite vSPSite = new SPSite(pSiteUrl))
               {
                  using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                  {
                     AddMember(pSiteUrl, pWebRelativeUrl, pGroupName, pAccountNameNew);
                     RemoveMember(pSiteUrl, pWebRelativeUrl, pGroupName, pAccountNameOld);
                  }
               }
            //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Group).Namespace, typeof(Group).Name, "ChangeMember", ex);
                throw;
            }
        }

        public static void AddMember(string pSiteUrl, string pWebRelativeUrl, string pGroupName, SPUser pSPUser)
        {
         try
         {
          
                     //SPSecurity.RunWithElevatedPrivileges(delegate()
                    // {
                        using (SPSite vSPSite = new SPSite(pSiteUrl))
                        {
                           using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                           {
                              if (vSPWeb.SiteGroups.OfType<SPGroup>().Where(x => x.Name.ToUpper() == pGroupName.ToUpper()).Any())
                              {

                                 SPGroup vSPGroup = vSPWeb.SiteGroups[pGroupName];

                                 if (!vSPGroup.Users.OfType<SPUser>().Where(x => x.LoginName.ToUpper() == pSPUser.LoginName.ToUpper()).Any())
                                 {
                                    vSPGroup.AddUser(pSPUser);
                                    vSPGroup.Update();
                                 }
                              }

                           }
                        }
                     //});
          
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Group).Namespace, typeof(Group).Name, "AddMember", ex);
                throw;
            }
        }

        public static bool IsMember(string pSiteUrl, string pWebRelativeUrl, string pGroupName, SPUser pSPUser)
        {
            try
            {
                bool vReturn = false;

                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                        {
                            if (vSPWeb.SiteGroups.OfType<SPGroup>().Where(x => x.Name.ToUpper() == pGroupName.ToUpper()).Any())
                            {
                                SPGroup vSPGroup = vSPWeb.SiteGroups[pGroupName];

                                vReturn = vSPGroup.Users.OfType<SPUser>().Where(x => x.LoginName.ToUpper() == pSPUser.LoginName.ToUpper()).Any();
                            }

                        }
                    }
                //});

                return vReturn;

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Group).Namespace, typeof(Group).Name, "AddMember", ex);
                throw;
            }
        }

        public static void RemoveMember(string pSiteUrl, string pWebRelativeUrl, string pGroupName, SPUser pSPUser)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                        {
                            if (vSPWeb.SiteGroups.OfType<SPGroup>().Where(x => x.Name.ToUpper() == pGroupName.ToUpper()).Any())
                            {
                                SPGroup vSPGroup = vSPWeb.SiteGroups[pGroupName];

                                if (vSPGroup.Users.OfType<SPUser>().Where(x => x.LoginName.ToUpper() == pSPUser.LoginName.ToUpper()).Any())
                                {
                                    vSPGroup.RemoveUser(pSPUser);
                                    vSPGroup.Update();
                                }
                            }

                        }
                    }
                //});

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Group).Namespace, typeof(Group).Name, "RemoveMember", ex);
                throw;
            }
        }

        public static void ChangeMember(string pSiteUrl, string pWebRelativeUrl, string pGroupName, SPUser pSPUserOld, SPUser pSPUserNew)
        {
         try
         {
            //SPSecurity.RunWithElevatedPrivileges(delegate()
            //{
               using (SPSite vSPSite = new SPSite(pSiteUrl))
               {
                  using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                  {
                     AddMember(pSiteUrl, pWebRelativeUrl, pGroupName, pSPUserNew);
                     RemoveMember(pSiteUrl, pWebRelativeUrl, pGroupName, pSPUserOld);
                  }
               }
            //});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Group).Namespace, typeof(Group).Name, "ChangeMember", ex);
                throw;
            }
        }
    }
}
